package com.sgb.kjwl.view.ui.activity.self.authentication;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityTeamAuthListBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.TeamAuthListEntity;
import com.sgb.kjwl.model.entity.UserAuthMsgEntity;
import com.sgb.kjwl.model.entity.UserMsgEntity;
import com.sgb.kjwl.model.entity.self.ComAuthListEntity;
import com.sgb.kjwl.model.entity.self.OrganizationAuthEntity;
import com.sgb.kjwl.view.ui.activity.self.userInfo.RegisterActivity3;
import com.sgb.kjwl.view.ui.adapter.self.ComAuthListAdapter;
import com.sgb.kjwl.view.ui.adapter.self.OrganizationAuthListAdapter;
import com.sgb.kjwl.view.ui.adapter.self.TeamAuthListAdapter;
import com.sgb.kjwl.viewmodel.self.TeamAuthListModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MToast;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;


/**
 * @版本 用户中心1.3.8
 * @产品 张彬
 * @页面 团队认证列表，企业认证列表
 * @作者 陈方凯
 */
public class TeamAuthListActivity extends AppActivity implements ItemInputTextView.OnWholeItemClickListener {
    private ActivityTeamAuthListBinding binding;
    private TeamAuthListAdapter teamAuthListAdapter;
    private OrganizationAuthListAdapter organizationAuthListAdapter;
    private ComAuthListAdapter comAuthListAdapter;
    private Map<String, Object> map = new HashMap<>();
    private int type;
    private String userNo;
    private UserAuthMsgEntity data;
    @Inject
    TeamAuthListModel viewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    //设置状态栏为暗
    @Override
    protected boolean statusBarDark() {
        return false;
    }

    //跳转
    public static void start(Context context, int type, String userNo, UserAuthMsgEntity data) {
        Intent intent = new Intent();
        intent.setClass(context, TeamAuthListActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        bundle.putString("userNo", userNo);
        bundle.putSerializable("data", data);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }


    @Override
    protected void initView() {
        //双向绑定Activity和View
        binding = DataBindingUtil.setContentView(TeamAuthListActivity.this, R.layout.activity_team_auth_list);
        //绑定ViewModel
        binding.setViewModel(viewModel);
        binding.include.rightView.setVisibility(View.VISIBLE);
        binding.include.rightView.setText("去认证");
        binding.include.rightView.setOnClickListener(this);
        type = getIntent().getIntExtra("type", 0);
        userNo = getIntent().getStringExtra("userNo");
        data = (UserAuthMsgEntity) getIntent().getSerializableExtra("data");
        //设置头部信息
        binding.include.titleView.setText("认证列表");
        //返回按钮的事件
        binding.include.ivBack.setOnClickListener(this);

        if (type == 1) {
            //公司列表
            if (comAuthListAdapter == null) {
                comAuthListAdapter = new ComAuthListAdapter(getActivity(), null);
            }
            binding.recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 1));

            binding.recyclerView.setAdapter(comAuthListAdapter);
            comAuthListAdapter.setOnItemClick(new AdapterOnItemClick<ComAuthListEntity>() {
                @Override
                public void onItemClick(ComAuthListEntity entity, int position) {
                    EnterpriseAuthDetailActivity.start(TeamAuthListActivity.this, entity.getId() + "");
                }
            });
        } else if (type == 0){
            //团队列表
            if (teamAuthListAdapter == null) {
                teamAuthListAdapter = new TeamAuthListAdapter(getActivity(), null);
            }
            binding.recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 1));

            binding.recyclerView.setAdapter(teamAuthListAdapter);
            teamAuthListAdapter.setOnItemClick(new AdapterOnItemClick<TeamAuthListEntity>() {
                @Override
                public void onItemClick(TeamAuthListEntity entity, int position) {
                    TeamAuthDetailActivity.start(TeamAuthListActivity.this, entity.getId() + "");
                }
            });
        } else {
            //组织列表
            if (organizationAuthListAdapter == null) {
                organizationAuthListAdapter = new OrganizationAuthListAdapter(getActivity(), R.layout.item_organization_auth_list, null);
            }
            binding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false));

            binding.recyclerView.setAdapter(organizationAuthListAdapter);
            organizationAuthListAdapter.setOnItemClick(new AdapterOnItemClick<OrganizationAuthEntity>() {
                @Override
                public void onItemClick(OrganizationAuthEntity entity, int position) {
                    OrganizationDetailActivity.Companion.launch(TeamAuthListActivity.this, entity);
                }
            });
        }
        //binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                isTeamOrCom(false);
            }

            @Override
            public void onLoadMore() {
                isTeamOrCom(true);
            }
        });
    }

    @Override
    protected void init() {
        isTeamOrCom(false);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().appComponent(appComponent).selfModule(new SelfModule(this)).build().inject(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        isTeamOrCom(false);
    }

    private void isTeamOrCom(boolean isLodMore) {
        if (type == 1) {
            viewModel.findMyCompanyList(isLodMore, userNo);
        } else if (type == 0){
            viewModel.findMyTeamList(isLodMore, userNo);
        } else if (type == 2){
            viewModel.findMyOrganizationList(isLodMore);
        }
    }

    /**
     * 设置团队列表数据
     *
     * @param list
     * @param isLoadMore
     */
    public void showTeamListData(List<TeamAuthListEntity> list, boolean isLoadMore) {
        binding.recyclerView.refreshComplete();
        binding.recyclerView.setLoadingMoreEnabled(list != null && list.size() >= 20);
        if (isLoadMore) {
            if (null != list && list.size() > 0) {
                binding.recyclerView.loadMoreComplete();
                teamAuthListAdapter.addDatas(list);
            } else {
                binding.recyclerView.setNoMore(true);
            }
            binding.recyclerView.setVisibility(View.VISIBLE);
        } else {
            binding.recyclerView.refreshComplete();
            if (null != list && list.size() > 0) {
                binding.recyclerView.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                teamAuthListAdapter.setDatas(list);
                //binding.recyclerView.setLoadingMoreEnabled(true);
            } else {
                teamAuthListAdapter.setDatasMayNull(null);
                binding.recyclerView.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    /**
     * 设置公司列表数据
     *
     * @param list
     * @param isLoadMore
     */
    public void showComListData(List<ComAuthListEntity> list, boolean isLoadMore) {
        binding.recyclerView.refreshComplete();
        binding.recyclerView.setLoadingMoreEnabled(list != null && list.size() >= 20);
        if (isLoadMore) {
            if (null != list && list.size() > 0) {
                binding.recyclerView.loadMoreComplete();
                comAuthListAdapter.addDatas(list);
            } else {
                binding.recyclerView.setNoMore(true);
            }
            binding.recyclerView.setVisibility(View.VISIBLE);
        } else {
            binding.recyclerView.refreshComplete();
            if (null != list && list.size() > 0) {
                binding.recyclerView.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                comAuthListAdapter.setDatas(list);
                //binding.recyclerView.setLoadingMoreEnabled(true);
            } else {
                comAuthListAdapter.setDatasMayNull(null);
                binding.recyclerView.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void OnWholeItemClick(View view) {
        switch (view.getId()) {
            case R.id.item_tv_model:
                break;
        }
    }

    //点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //返回按钮
            case R.id.iv_back:
                //关闭当前界面
                finish();
                break;
            case R.id.right_view:
                if (type == 0) {
//                    if (!data.isPersonAuth()) {
//                        MToast.showToast(TeamAuthListActivity.this, "请先完成个人认证！");
//                    } else {
                        TeamAuthActivity.start(TeamAuthListActivity.this, data.getRealName(), data.getIdCard(), data.getTelPhone());
                    //}
                } else if (type == 1){
//                    if (!data.isPersonAuth()) {
//                        MToast.showToast(TeamAuthListActivity.this, "请先完成个人认证！");
//                    } else {
                        RegisterActivity3.start(TeamAuthListActivity.this, true);
                    //}
                } else if (type == 2){
//                    if (!data.isPersonAuth()) {
//                        MToast.showToast(TeamAuthListActivity.this, "请先完成个人认证！");
//                    } else {
                        OrganizationAuthActivity.launch(TeamAuthListActivity.this);
                    //}
                }
                break;
        }
    }

    //组织认证列表
    public void showOrganizationListData(List<OrganizationAuthEntity> list, boolean isLoadMore) {
        binding.recyclerView.refreshComplete();
        binding.recyclerView.setLoadingMoreEnabled(list != null && list.size() >= 20);
        if (isLoadMore) {
            if (null != list && list.size() > 0) {
                binding.recyclerView.loadMoreComplete();
                organizationAuthListAdapter.addDatas(list);
            } else {
                binding.recyclerView.setNoMore(true);
            }
            binding.recyclerView.setVisibility(View.VISIBLE);
        } else {
            binding.recyclerView.refreshComplete();
            if (null != list && list.size() > 0) {
                binding.recyclerView.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                organizationAuthListAdapter.setDatas(list);
                //binding.recyclerView.setLoadingMoreEnabled(true);
            } else {
                organizationAuthListAdapter.setDatasMayNull(null);
                binding.recyclerView.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }
}
